package com.solomon.travel.common.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.solomon.travel.common.base.controller.BaseController;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Announce;
import com.solomon.travel.common.service.AnnounceService;
import com.solomon.travel.common.util.ConstantUtils;
import com.solomon.travel.common.util.IdUtils;
import com.solomon.travel.common.util.PageUtils;

@RestController
@RequestMapping("/web/announce")
public class AnnounceController extends BaseController{
	
	@Resource
	private AnnounceService announceService;
	
	@RequestMapping("/publish")
	public ApiResponse publish(Announce announce) throws Exception{
		announce.setObjectId(IdUtils.uuid());
		announce.setCreatedAt(new Date());
		announce.setUpdatedAt(announce.getCreatedAt());
		announce.setFromUser(currentUser().getObjectId());
		announceService.publish(announce);
		return new ApiResponse(200);
	}
	
	@RequestMapping("/list")
	public ApiResponse list(Integer type,Page page) {
		int counts = announceService.countByType(type);
		page = PageUtils.createPage(page, counts);
		List<Announce> announces = announceService.listByType(currentUser().getType() == 0 ? null:currentUser().getObjectId(),type, page);
		HashMap<String, Object> result = new HashMap<>();
		result.put("annouces", announces);
		result.put("page", page);
		return new ApiResponse(200, result);	
	}
	
	@RequestMapping("/detail")
	public ApiResponse detail(String announceId) {
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, announceService.getById(announceId));
	}
	
	@RequestMapping("/update")
	public ApiResponse update(Announce announce) {
		announceService.update(announce);
		return defaultResponse();
	}
	
	@RequestMapping("/delete")
	public ApiResponse delete(String announceId) {
		announceService.delete(announceId);
		return defaultResponse();
	}
}
